package com.glsc.ngateway.common.base.repo.mysql.flowable;

import com.glsc.ngateway.common.base.domain.mysql.flowable.ActHiTaskinst;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lium
 * @date 2022/2/7
 */
public interface FlowableCommonRepo extends JpaRepository<ActHiTaskinst, Long>, JpaSpecificationExecutor<ActHiTaskinst> {


    @Query(value = "SELECT DISTINCT t3.PROC_DEF_ID_ as processDefinitionId,t4.NAME_ as processDefinitionName, t3.START_USER_ID_ as startUserId,t3.START_TIME_ as startTime, " +
            "t3.NAME_ AS title, t3.PROC_INST_ID_ AS processInstanceId, t3.TENANT_ID_ as tenantId, t3.BUSINESS_KEY_ as businessKey, t3.SUPER_PROCESS_INSTANCE_ID_ as supperProcessInstanceId " +
            "FROM ACT_HI_TASKINST t1 " +
            "JOIN ACT_RE_PROCDEF t4 ON t4.ID_ = t1.PROC_DEF_ID_ " +
            "LEFT JOIN ACT_HI_PROCINST t3 ON t1.PROC_INST_ID_ = t3.PROC_INST_ID_ " +
            "WHERE t1.END_TIME_ is not null " +
            "AND t1.ASSIGNEE_  = :involveUser " +
            "AND (:processIdListStr IS NULL OR t1.PROC_INST_ID_ in (:processIdList))  " +
            "AND (:processIdNotListStr IS NULL OR t1.PROC_INST_ID_ not in (:processIdNotList))  " +
            "AND (:sysid IS NULL OR t3.TENANT_ID_ = :sysid ) " +
            "and (:processDefinitionKeyStr IS NULL OR t4.KEY_  in :processDefinitionKey ) " +
            "and (:title IS NULL OR t3.NAME_ LIKE CONCAT('%',:title,'%') ) " +
            "and (:begin IS NULL OR t3.START_TIME_ >= :begin ) " +
            "and (:end IS NULL OR t3.START_TIME_ <= :end ) " +
            "and (:hasEndForInvolve IS NULL OR (if(:hasEndForInvolve='1', t3.END_TIME_ is not null , t3.END_TIME_ is null ) ) ) " +
            "ORDER by t3.START_TIME_ desc "
            , countQuery = "SELECT count(DISTINCT t3.PROC_INST_ID_ ) " +
            "FROM ACT_HI_TASKINST t1 " +
            "JOIN ACT_RE_PROCDEF t4 ON t4.ID_ = t1.PROC_DEF_ID_ " +
            "LEFT JOIN ACT_HI_PROCINST t3 ON t1.PROC_INST_ID_ = t3.PROC_INST_ID_ " +
            "WHERE t1.END_TIME_ is not null " +
            "AND t1.ASSIGNEE_  = :involveUser " +
            "AND (:processIdListStr IS NULL OR t1.PROC_INST_ID_ in (:processIdList)) " +
            "AND (:processIdNotListStr IS NULL OR t1.PROC_INST_ID_ not in (:processIdNotList))  " +
            "AND (:sysid IS NULL OR t3.TENANT_ID_ = :sysid ) " +
            "and (:processDefinitionKeyStr IS NULL OR t4.KEY_  in :processDefinitionKey ) " +
            "and (:title IS NULL OR t3.NAME_ LIKE CONCAT('%',:title,'%')  ) " +
            "and (:begin IS NULL OR t3.START_TIME_ >= :begin ) " +
            "and (:end IS NULL OR t3.START_TIME_ <= :end ) " +
            "and (:hasEndForInvolve IS NULL OR (if(:hasEndForInvolve='1', t3.END_TIME_ is not null , t3.END_TIME_ is null ) ) ) " +
            "ORDER by t3.START_TIME_ desc ",
            nativeQuery = true)
    Page<List<Map<String, String>>> findUserDone(@Param("involveUser") String involveUser
            , @Param("processDefinitionKeyStr") String processDefinitionKeyStr
            , @Param("processDefinitionKey") List<String> processDefinitionKey
            , @Param("title") String title
            , @Param("begin") Date begin, @Param("end") Date end
            , @Param("sysid") String sysid
            , @Param("hasEndForInvolve") String hasEndForInvolve
            , @Param("processIdList") List<String> processIdList
            , @Param("processIdListStr") String processIdListStr
            , @Param("processIdNotList") List<String> processIdNotList
            , @Param("processIdNotListStr") String processIdNotListStr
            , Pageable pageable);

    @Query(value = "SELECT DISTINCT t3.NAME_ AS processTitle, t3.PROC_INST_ID_ AS processId FROM ACT_HI_TASKINST t1 LEFT JOIN ACT_HI_PROCINST t3 ON t1.PROC_INST_ID_ = t3.PROC_INST_ID_ " +
            "WHERE t1.ASSIGNEE_ = :user and t3.TENANT_ID_ = :sysId and t3.PROC_INST_ID_ in (:processIdList)", nativeQuery = true)
    List<Map<String, String>> findUserParticipateProcessList(List<String> processIdList, String user, String sysId);



    @Modifying(clearAutomatically=true, flushAutomatically = true)
    @Query(value = " UPDATE flowabledb.ACT_HI_PROCINST  set NAME_ = :processTitle  WHERE ID_ = :processId ", nativeQuery = true)
    Integer updateHistoryEndProcessTitle(@Param("processId") String processId, @Param("processTitle") String processTitle);
}
